/***
 *  权限管理使用变量
 * 
 * 
 */
//import   //从路由中获取信息
import {getMenu} from '@/http/api'
import Router,{mo_routes} from '@/router/index'
//import r from 
import {getRouter,setDefaultRouter} from '@/router/api'
import { menuData } from '@/moData';


export default ({
    namespace:true,
    state: {
        resRouter:null, //全部路由
        sideMenu:[],    //导航菜单
        currentMenu:'', //当前菜单（高亮
    },
    getters: {
    },
    mutations: {
        SET_PERMISSION(state:any,routers:any){
            state.resRouter = routers;
        },
        CLEAR_PERMISSION(state:any){
            state.resRouter = null;
        },
        SET_MENU(state:any,menu:any){
            state.sideMenu = menu;
        },
        CLEAR_MENU(state:any){
            state.sideMenu = [];
        },
    },
    actions: {  //存放异步的函数
        async FETCH_PERMISSION(commit:any ,state :any ){
                 let res = await getMenu();
                debugger ;
                let userRouter =  res.data.menuData;
                let allRouters = mo_routes ;
                let routes = getRouter(userRouter,allRouters);
                let MainContainer = mo_routes.find(v => v.path === "") ;
                let children = MainContainer? MainContainer.children : [];
                children = children ? children : [];
                children.push( ...routes);

                //----生成菜单--------
                commit('SET_MENU',children);

                //----设置默认路由-
                setDefaultRouter(MainContainer);

                //----初始化路由------
                let initRouters = Router.options.routes ;
                if(MainContainer)Router.addRoute(MainContainer);
                commit('SET_PERMISSION',{...initRouters,...allRouters});
            
    
        } 

    },
    modules: {

    }
  })
  